package com.xunfang.portal.controller;

import com.xunfang.portal.model.entity.China;
import com.xunfang.portal.model.request.AddressRequest;
import com.xunfang.portal.model.vo.AddressVo;
import com.xunfang.portal.service.AddressService;
import com.xunfang.portal.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Tag(name = "收货地址模块")
@Slf4j
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private JwtUtils jwtUtil;

    /**
     * 通过用户id获取当前用户的收货地址
     */
    @GetMapping("/findAddress")
    @Operation(summary = "获取当前用户的收货地址")
    public ResponseEntity<List<AddressVo>> findAddress(@RequestHeader(name = "Authorization", required = false) String token) {
        //获取当前登录用户的id
        String userId = jwtUtil.getValue(token, "userId");
        //获取当前用户的地址列表
        List<AddressVo> list = addressService.findByUserId(Long.parseLong(userId));
        return ResponseEntity.ok(list);
    }

    //查询省
    @GetMapping("/getAllProvince")
    @Operation(summary = "获取所有省份")
    public ResponseEntity<List<China>> getAllProvince() {
        List<China> list = addressService.getAllProvince();
        log.info("获取所有省份: {}", list);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据省份id，查询城市
     *
     *     swagger 注解
     *         @Tag(name = "收货地址模块")                        类上面的注解\在线文档说这个类的功能
     *         @Operation(summary = "根据省份id，查询城市")       方法上面的注解\在线文档说这个方法的功能
     *         @Parameter(name = "provinceId", description= "省份id", required = true)
     *                                                           参数上面的注解\在线文档说这个参数的作用
     */
    @GetMapping("/getAllCities")
    @Operation(summary = "根据省份id，查询城市")
    @Parameter(name = "provinceId", description= "省份id", required = true)
    public ResponseEntity<List<China>> getAllCities(Integer provinceId) {
        log.info("省份id：{}", provinceId);
        List<China> list = addressService.getAllCities(provinceId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据省份id，查询城市
     */
    @GetMapping("/getAllDistricts")
    @Operation(summary = "根据城市id，查询区/县")
    @Parameter(name = "cityId", description = "城市id", required = true,
            schema  = @Schema(
                    type="integer"
            ))
    public ResponseEntity<List<China>> getAllDistricts(Integer cityId) {
        log.info("城市id：{}", cityId);
        List<China> list = addressService.getAllDistricts(cityId);
        return ResponseEntity.ok(list);
    }

    /**
     * 保存地址
     */
    @PostMapping("/save")
    @Operation(summary = "保存地址")
    public ResponseEntity save(@RequestBody AddressRequest addressRequest, @RequestHeader("Authorization") String token) {
        // 获取当前登录的用户id
        String userId = jwtUtil.getValue(token, "userId");
        addressService.addAddress(addressRequest, Long.valueOf(userId));
        return ResponseEntity.ok("添加成功...");
    }
}